<?php
function token(){
    $appid = 'wx9405afef4dd60056';
    $appsecret = '8b3292b12b035ff6fd91e2d795951f04';
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
    $contents = file_get_contents($url);
    $data = json_decode($contents, true);
    if(isset($data['access_token'])){
        return $data['access_token'];
    }
}

function post($url,$postData){
    if(is_array($postData)){
        $postData = json_encode($postData);
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    // curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$postData );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json', // 设置内容类型为JSON
        // 'Content-Length: ' . strlen(json_encode($postData)) // 设置内容长度
    ]);
    $result = curl_exec($ch);
    if(curl_errno($ch)){
        echo 'Curl error: ' . curl_error($ch);
    }
    curl_close($ch);
    return $result;
}

//获取ShortLink
function shortLink($name){
    $name = urlencode($name);
    $name = str_replace('+','%20', $name);
    $url = "https://api.weixin.qq.com/wxa/genwxashortlink?access_token=".token();
    $postData = [
        'page_url' => 'pages/newsArticle/webUrl?url=https://dy.tbpdd.top/'.$name,
        'is_permanent' => false,
    ];

    return post($url,$postData);
}


//获取电视剧列表
$api = 'https://video.tbpdd.top/api/fs/list';
$dsjList = file_get_contents($api);
$rows = json_decode($dsjList,true)['data']['content'];
foreach ($rows as $row) {
    $name = $row['name'];
    $data = [
        "path"=> "$name",
        "password"=> "",
        "page"=> 1,
        "per_page"=> 0,
        "refresh"=> false
    ];
    $detail = post($api,$data);
    $total = json_decode($detail,true)['data']['total'];
    // echo($total);


    $link = shortLink($name);
    $linkD = json_decode($link,true);
    if(isset($linkD['link'])){
        $mp = str_replace('#小程序://科慧花园','mp:/',$linkD['link']);
        $files[] = "$name 更新$total\n👉$mp";

    }
}

$contents = implode("\n",$files);
file_put_contents('video.txt',$contents);
echo $contents;
